import EventApi from '@/api/event';
import { addBm }  from '@/api/bm';
import { Toast } from 'vant';
export default {
    data(){
        return {
            a:'himan',
            detail:{},
            formVal:{ },
            cusForm:[]
        }
    },
    created(){
        this.getDetailInfo();

        for(const item of this.cusForm){
            this.$set(this.formVal,item.varname,'')
        }
       
    },
 
    computed:{
        status(){
            const now = Date.now();
            const start = new Date(this.detail.stime).getTime(); // 比赛开始的时间戳
            const etart = new Date(this.detail.etime).getTime(); // 比赛结束的时间戳
            const enrollStime = new Date(this.detail.enrollStime).getTime(); // 报名开始的时间戳
            const enrollEtime = new Date(this.detail.enrollEtime).getTime(); // 报名结束的时间戳

             if(now < enrollStime)  return '报名未开始';
             if(now > enrollStime  &&  now < enrollEtime)  return '报名中';
             if(now > enrollEtime  &&  now < start)  return '即将开赛';
            if(now > start  &&  now < etart)  return '比赛中';
            if(now > etart)  return '已结束';

        }
    },
    methods:{
        // 立即报名
        async handleJoing(){
            const eventid = this.$route.query.id;
            const uid = 5 ; // hard code 为 5
            const info = JSON.stringify(this.formVal);
            const res = await addBm({
                eventid,
                uid,
                info
            })

            const {  status ,data } = res;
            if(status === 201 ){
                Toast.success('报名成功');
            } else {
                Toast.fail('报名失败');
            }
        },
        async getDetailInfo(){
            const id = this.$route.query.id
            let res = await EventApi.getEvent(id)
            console.log('res',res)
            this.detail = res.data
            const _formdata = JSON.parse( res.data.cusForm );
            for(const item of _formdata){
                if(item.type === 'date'){
                    item.show = false
                }
            }
            this.cusForm = _formdata
        }
    }
}